unit Con_Departamento;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Consulta, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids,
  Db, Dbtables, TFlatButtonUnit;

type
  TFrmCon_Departamento = class(TFrmConsulta)
    Label1: TLabel;
    EdtDescricao: TEdit;
    dbgDepartamento: TDBGrid;
    btnGravar: TFlatButton;
    procedure BtnFecharClick(Sender: TObject);
    procedure EdtDescricaoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);

  private
    { Private declarations }
    procedure FazConsulta;

  public
    { Public declarations }
    Dep_codigo: string;
    cadastrando: boolean;

  end;

var
  FrmCon_Departamento: TFrmCon_Departamento;

implementation

uses
  Dm, Biblio, clsDepartamento, Cad_Departamento;

{$R *.DFM}

var
  Departamento: TDepartamento;
  tds: Tdatasource;
  tq: Tquery;

procedure TFrmCon_Departamento.BtnFecharClick(Sender: TObject);
begin
  inherited;
  Dep_codigo := tq.fieldbyname('Dep_codigo').asstring;
end;

procedure TFrmCon_Departamento.EdtDescricaoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key = 12 then begin
    FazConsulta;
    EdtDescricao.setfocus;
  end;
end;

procedure TFrmCon_Departamento.FormActivate(Sender: TObject);
begin
  inherited;
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tds.DataSet := tq;
  dbgDepartamento.DataSource := tds;
  fazconsulta;
  EdtDescricao.setfocus;
  btnGravar.Visible := not(cadastrando);

end;

procedure TFrmCon_Departamento.FazConsulta;
begin
  if EdtDescricao.text = '' then begin
    Departamento.Consultar(' ', tq);
  end else begin
    Departamento.Consultar(EdtDescricao.text, tq);
  end;
  tq.open;
end;

procedure TFrmCon_Departamento.FormCreate(Sender: TObject);
begin
  inherited;
  tds := TDataSource.create(self);
  tq := Tquery.create(self);
  Departamento := TDepartamento.Create;
end;

procedure TFrmCon_Departamento.FormDestroy(Sender: TObject);
begin
  inherited;
  Departamento.free;
  tds.free;
  tq.free;
end;

procedure TFrmCon_Departamento.btnGravarClick(Sender: TObject);
var
  frm:TfrmCad_Departamento;

begin
  inherited;
  frm := TFrmCad_Departamento.Create(nil);
  try
    frm.consultando := true;
    frm.ShowModal;
  finally
    frm.Free;
  end;

  FazConsulta;

end;

end.

